package com.gqj.bas.controller;

import com.gqj.bas.domain.BasWarehouse;
import com.gqj.bas.service.IBasWarehouseService;
import com.gqj.common.annotation.Log;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 仓库Controller
 * 
 * @author lxq
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/bas/basWarehouse")
public class BasWarehouseController extends BaseController
{
    @Autowired
    private IBasWarehouseService basWarehouseService;

    /**
     * 查询仓库列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basWarehouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasWarehouse basWarehouse)
    {
        startPage();
        basWarehouse.setDelFlag("0");
        List<BasWarehouse> list = basWarehouseService.selectBasWarehouseList(basWarehouse);
        return getDataTable(list);
    }

    /**
     * 查询库房列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basDevice:warehouseOptions')")
    @GetMapping("/warehouseOptions")
    public AjaxResult warehouseOptions(BasWarehouse basWarehouse)
    {
        List<Map<String, Object>> list = basWarehouseService.warehouseOptions(basWarehouse);
        return AjaxResult.success(list);
    }

    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basWarehouse:userOptions')")
    @GetMapping("/userOptions")
    public AjaxResult userOptions(BasWarehouse basWarehouse)
    {
        List<Map<String, Object>> list = basWarehouseService.userOptions(basWarehouse);
        return AjaxResult.success(list);
    }

    /**
     * 导出仓库列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basWarehouse:export')")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasWarehouse basWarehouse)
    {
        List<BasWarehouse> list = basWarehouseService.selectBasWarehouseList(basWarehouse);
        ExcelUtil<BasWarehouse> util = new ExcelUtil<BasWarehouse>(BasWarehouse.class);
        util.exportExcel(response, list, "仓库数据");
    }

    /**
     * 获取仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basWarehouse:query')")
    @GetMapping(value = "/{warehouseId}")
    public AjaxResult getInfo(@PathVariable("warehouseId") Long warehouseId)
    {
        return AjaxResult.success(basWarehouseService.selectBasWarehouseByWarehouseId(warehouseId));
    }

    /**
     * 新增仓库
     */
    @PreAuthorize("@ss.hasPermi('bas:basWarehouse:add')")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasWarehouse basWarehouse)
    {
        if (UserConstants.NOT_UNIQUE.equals(basWarehouseService.checkWarehouseNameUnique(basWarehouse)))
        {
            return AjaxResult.error("新增库房'" + basWarehouse.getWarehouseName() + "'失败，库房名称已存在");
        }
        basWarehouse.setCreateBy(getUsername());
        return toAjax(basWarehouseService.insertBasWarehouse(basWarehouse));
    }

    /**
     * 修改仓库
     */
    @PreAuthorize("@ss.hasPermi('bas:basWarehouse:edit')")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasWarehouse basWarehouse)
    {
        if (UserConstants.NOT_UNIQUE.equals(basWarehouseService.checkWarehouseNameUnique(basWarehouse)))
        {
            return AjaxResult.error("修改库房'" + basWarehouse.getWarehouseName() + "'失败，库房名称已存在");
        }
        basWarehouse.setUpdateBy(getUsername());
        return toAjax(basWarehouseService.updateBasWarehouse(basWarehouse));
    }

    /**
     * 删除仓库
     */
    @PreAuthorize("@ss.hasPermi('bas:basWarehouse:remove')")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{warehouseIds}")
    public AjaxResult remove(@PathVariable Long[] warehouseIds)
    {
        for(Long warehouseId: warehouseIds){
            if (basWarehouseService.checkMaterialExistWarehouse(warehouseId) || basWarehouseService.checkDeviceExistWarehouse(warehouseId))
            {
                return AjaxResult.error("库房已被引用,不允许删除");
            }
        }
        return toAjax(basWarehouseService.deleteBasWarehouseByWarehouseIds(warehouseIds));
    }
}
